package com.zboot.zbootadmin.project.system.log.controller;

import com.zboot.zbootadmin.common.annotation.Log;
import com.zboot.zbootadmin.common.constant.HttpStatus;
import com.zboot.zbootadmin.common.domain.ResponseData;
import com.zboot.zbootadmin.common.utils.ExcelUtils;
import com.zboot.zbootadmin.project.system.log.entity.SysLogninIformation;
import com.zboot.zbootadmin.project.system.log.entity.dto.LoginInfoDto;
import com.zboot.zbootadmin.project.system.log.service.SysLoginInformationService;
import com.zboot.zbootadmin.project.system.user.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 系统访问记录 前端控制器
 * </p>
 *
 * @author zyt
 * @since 2020-11-15
 */
@RestController
@RequestMapping("/sysLoginInformation")
public class SysLoginInformationController {

    @Autowired
    private SysLoginInformationService loginInformationService;

    /**
     *
     * @Description: 展示登陆日志（包括查询）
     *
     * @author: zyt
     * @date: 20:40 2020/11/18
     * @param: [loginInfoDto]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("登陆信息")
    @PreAuthorize("@zboot.hasPermi('sys:log:loginuserlog')")
    @PostMapping("/list")
    public ResponseData list( @RequestBody LoginInfoDto loginInfoDto){
        return new ResponseData(HttpStatus.SUCCESS,loginInformationService.getLoginInfo(loginInfoDto));
    }

    /**
     *
     * @Description: 删除登陆日志信息（包括批量删除）
     *
     * @author: zyt
     * @date: 20:40 2020/11/18
     * @param: [infoIds]
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("删除日志")
    @PreAuthorize("@zboot.hasPermi('sys:log:delete')")
    @DeleteMapping("/deleteInfo")
    public ResponseData deleteInfo(  @RequestBody String[] infoIds){
        return new ResponseData(HttpStatus.SUCCESS,loginInformationService.removeByIds(Arrays.asList(infoIds)));
    }

    /**
     *
     * @Description: 清除表
     *
     * @author: zyt
     * @date: 20:41 2020/11/18
     * @param: []
     * @return: com.zboot.zbootadmin.common.domain.ResponseData
     *
     */
    @Log("清除表")
    @PreAuthorize("@zboot.hasPermi('sys:log:clear')")
    @DeleteMapping("/clearInfo")
    public ResponseData clearInfo( ){
        loginInformationService.clearInfo();
        return new ResponseData(HttpStatus.SUCCESS,"清除成功");
    }

    @Log("导出")
    @PreAuthorize("@zboot.hasPermi('sys:log:export')")
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        String [] titles = {"访问ID","用户账号","登录IP地址",
                "登录地点","浏览器类型","操作系统","登录状态（0成功 1失败）","提示消息","访问时间"};
        List<SysLogninIformation> logninIformations = loginInformationService.list();
        ExcelUtils<SysLogninIformation> excelUtils = new ExcelUtils<>();
        String [] properties = {"infoId","userName","ipaddress","loginLocation","browser","os","status","msg","loginTime"};
        excelUtils.downExcel(titles,logninIformations,response,SysLogninIformation.class,properties);
    }

}
